<?php
/**
 * This file is included into amember/thanks.html and cc/cc_info.html
 * for displaying of payment receipt information
 * @var Invoice $invoice
 */
?>
<?php echo $this->blocks('receipt/before', '%s', ['invoice' => $invoice]) ?>
<?php
// If there was no payment passed (free trial or before payment) and next payment for invoice will be first or if payment was passed and it is first itself.
if(!empty($receiptAfterPayment)){
    $rcpt_period = empty($payment) || ($invoice->first_total>0 && $invoice->isFirstPayment($payment)) ? 'first' : 'second';
}else{
    $rcpt_period = $invoice->isFirstPayment() ? 'first' : 'second';
}
$rcpt_taxes = [];
foreach ($invoice->getItems() as $item) {
    if ($item->tax_rate && $item->{$rcpt_period."_tax"}) {
        if (!isset($rcpt_taxes[$item->tax_rate])) {
            $rcpt_taxes[$item->tax_rate] = 0;
        }
        $rcpt_taxes[$item->tax_rate] += $item->{$rcpt_period."_tax"};
    }
}
if (!$rcpt_taxes) {
    $rcpt_taxes[$invoice->tax_rate] = $invoice->{$rcpt_period."_tax"};
}
?>
<div class="am-receipt">
    <div class="am-receipt-content">
        <table class="am-receipt-table">
            <thead>
                <tr class="am-receipt-am-row-head">
                    <th class="am-receipt-product"><?php __e('Subscription/Product Title') ?></th>
                    <th class="am-receipt-qty"><?php __e('Qty') ?></th>
                    <th class="am-receipt-unit-price"><?php __e('Unit Price') ?></th>
                    <th class="am-receipt-price"><?php __e('Total Price') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($invoice->getItems() as $p): ?>
                <tr>
                    <td>
                        <?php p($p->item_title)?>
                        <!-- options -->
                        <div class="am-receipt-product-options">
                        <?php foreach ($p->getOptions() as $optKey => $opt): ?>
                            <br /><b><i><?php echo __e($opt['optionLabel']) ?>:</i></b>
                                 <i><?php p(implode(', ', array_map('strip_tags', (array)$opt['valueLabel']))) ?></i>
                                 <?php if ($opt['first_price'] || $opt['second_price']) : ?>
                                 <i style='color: gray;'><?= $opt['first_price'] > 0 ? '+' : '&minus;' ?> <?php
                                        $o = new stdclass;
                                        $o->first_price = $opt['first_price'];
                                        $o->second_price = $opt['second_price'];
                                        $o->first_period = $invoice->first_period;
                                        $o->second_period = $invoice->second_period;
                                        $o->rebill_times = $invoice->rebill_times;
                                        $o->currency = $invoice->currency;
                                        $t = new Am_TermsText($o);
                                    echo $t->getStringForOption(); ?></i>
                                 <?php endif ?>
                        <?php endforeach // item->getOptions() ?>
                        </div>
                    </td>
                    <td class="am-receipt-qty">
                        <?php p($p->qty) ?>
                    </td>
                    <td class="am-receipt-unit-price">
                        <?php p($invoice->getCurrency($p->{$rcpt_period."_price"})) ?>
                    </td>
                    <td class="am-receipt-price">
                            <?php if ($p->{$rcpt_period."_discount"} > 0): ?>
                        <span class="am-receipt-discounted-price"><?php echo $invoice->getCurrency($p->{"get".ucfirst($rcpt_period)."Subtotal"}())?></span>
                            <?php endif ?>
                            <?php echo $invoice->getCurrency($p->{"get".ucfirst($rcpt_period)."Total"}()) ?>
                    </td>
                </tr>
                <?php endforeach ?>
                <tr class="am-receipt-row-delimiter">
                    <td colspan="4"></td>
                </tr>
                <tr>
                    <td class="am-receipt-indent"></td>
                    <td class="am-receipt-total" colspan="2"><strong><?php __e('Subtotal') ?></strong></td>
                    <td class="am-receipt-total am-receipt-price">
                        <strong><?php echo $invoice->getCurrency($invoice->{$rcpt_period."_subtotal"}) ?></strong>
                    </td>
                </tr>
                <?php if ($invoice->{$rcpt_period."_discount"} > 0) : ?>
                <tr>
                    <td class="am-receipt-indent"></td>
                    <td colspan="2"><?php __e('Discount') ?></td>
                    <td class="am-receipt-price"><?php echo $invoice->getCurrency($invoice->{$rcpt_period."_discount"})?></td>
                </tr>
                <?php endif; ?>
                <?php if ($invoice->{$rcpt_period."_shipping"} > 0): ?>
                <tr>
                    <td class="am-receipt-indent"></td>
                    <td colspan="2"><?php __e('Shipping') ?></td>
                    <td class="am-receipt-price"><?php echo $invoice->getCurrency($invoice->{$rcpt_period."_shipping"})?></td>
                </tr>
                <?php endif ?>
                <?php if ($invoice->{$rcpt_period."_tax"} > 0): ?>
                <?php foreach ($rcpt_taxes as $rate => $total) : ?>
                <tr>
                    <td class="am-receipt-indent"></td>
                    <td colspan="2"><?php __e('Tax Amount') ?> (<?php p((float)$rate) ?>%)</td>
                    <td class="am-receipt-price"><?php echo $invoice->getCurrency($total)?></td>
                </tr>
                <?php endforeach; ?>
                <?php endif ?>
                <tr class="am-receipt-row-total">
                    <td class="am-receipt-indent"></td>
                    <td class="am-receipt-total" colspan="2"><strong><?php __e('Total') ?></strong></td>
                    <td class="am-receipt-total am-receipt-price">
                        <?php if ($invoice->{$rcpt_period."_discount"} > 0 && $invoice->{$rcpt_period."_tax"} == 0): ?>
                        <span class="am-receipt-discounted-price"><?php echo $invoice->getCurrency($invoice->{$rcpt_period."_subtotal"})?></span>
                        <?php endif ?>
                        <strong><?php echo $invoice->getCurrency($invoice->{$rcpt_period."_total"})?></strong>
                    </td>
                </tr>
                <tr class="am-receipt-row-terms">
                    <td colspan="4" class="am-receipt-terms <?= $invoice->rebill_times ? 'am-receipt-terms-subscription' : 'am-receipt-terms-one-time' ?>">
                        <strong><?php __e('Subscription Terms')?></strong>:
                        <?= ___($invoice->getTerms()); ?>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
<?php echo $this->blocks('receipt/after', '%s', ['invoice' => $invoice]) ?>